package com.example.cyw.mymap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener{
    MapView mMapView = null;
    MyLocationStyle myLocationStyle;
    AMap aMap;
    private EditText ed_key;
    private Button search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.myMap);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
//aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示，非必需设置。
        aMap.setMyLocationEnabled(true);

        CameraUpdate cameraUpdate= CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(23.18321,113.353111),15,30,0));
        aMap.moveCamera(cameraUpdate);
        aMap.setOnMapLongClickListener(this);
        initView();



    }

    private void initView() {
        ed_key= (EditText) findViewById(R.id.key);
        search= (Button) findViewById(R.id.search);

        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String key=ed_key.getText().toString();
                PoiSearch.Query query=new PoiSearch.Query(key,"","020");
                query.setPageSize(10);
                query.setPageNum(0);

                PoiSearch poisearch=new PoiSearch(MainActivity.this,query);
                poisearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(23.18321,113.353111),2000));
                poisearch.setOnPoiSearchListener(MainActivity.this);
                poisearch.searchPOIAsyn();
                Log.e("main","1");
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }


    @Override
    public void onMapLongClick(LatLng latLng) {
        Marker marker=aMap.addMarker(new MarkerOptions().position(latLng)
        .title("广交")
        .snippet("welcome")
        );
        marker.showInfoWindow();

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
   if (poiResult==null){
       Toast.makeText(MainActivity.this, "null", Toast.LENGTH_SHORT).show();
       return;
   }
        ArrayList<PoiItem> poiltemlist=poiResult.getPois();
        for (PoiItem poitem:poiltemlist){
            double laitude=poitem.getLatLonPoint().getLatitude();
            double longitdue=poitem.getLatLonPoint().getLongitude();

            LatLng latLng=new LatLng(laitude,longitdue);
            aMap.addMarker(new MarkerOptions().position(latLng)
            .title(poitem.getTitle())
            .snippet(poitem.getTel()));
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
